#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main(int argc, char const *argv[])
{
    pid_t pid = fork();
    if (pid == -1) {
        perror("fork error \n");
    } else if (pid == 0) {
        // 子进程
        printf("child process \n");
        sleep(2);
        exit(88);
        printf(" child prcess exit <%d>", getpid());
    } else if (pid > 0 ) {
        // 父进程
        int status;
        pid_t rcpid = wait(&status);
        // 会阻塞父进程, 等待子进程状态变化， 自动释放资源
        if (rcpid == -1) {
            perror("wait error\n");
            exit(EXIT_FAILURE);
        }
        printf("wait child process exit <%d>, exit code = %d \n", rcpid, WEXITSTATUS(status));
    }

    printf(" process \n");
    return 0;
}
